package com.huawei.esdk.tp.common.local.impl.utils;

import javax.net.ssl.SSLHandshakeException;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.soap.SOAPFaultException;

import org.apache.cxf.binding.soap.SoapFault;

import com.huawei.esdk.tp.common.local.bean.TPSDKResponse;
import com.huawei.esdk.tp.common.local.constant.NativeConstant;

public abstract class ExceptionUtils {

	public static void processSoapException(
			TPSDKResponse<? extends Object> resp, Exception e)
	{
		
	    if(e instanceof WebServiceException)
        {
            WebServiceException webServiceException = (WebServiceException)e;
            Throwable fault = webServiceException.getCause();
            if(fault instanceof SoapFault)
            {
                SoapFault soapFault = (SoapFault)webServiceException.getCause();
                String errorCodeStr = soapFault.getFaultCode().getLocalPart();
                int errCode = 0;
                try
                {
                    errCode = Integer.parseInt(errorCodeStr);
                }
                catch(Exception exp)
                {
                    errCode = NativeConstant.NATIVE_EXCEPTION;
                }
                resp.setResultCode(errCode);
            } else
            {
                String faultMsg = fault.getMessage();
                if(faultMsg.equals("Connection refused: connect") || faultMsg.contains("Connection reset") || faultMsg.contains("404"))
                {
                    resp.setResultCode(NativeConstant.ERROR_CODE_NETWORK_ERROR);
                } 
                else if(faultMsg.contains("handshake_failure") || faultMsg.contains("trustAnchors parameter must be non-empty"))
                {
                    resp.setResultCode(NativeConstant.SSL_HANDSHAKE_FAILURE);
                } 
                else if(faultMsg.contains("no protocol"))
                {
                    resp.setResultCode(NativeConstant.NATIVE_URL_FORMAT_ERRORCODE);
                }
                else if(faultMsg.contains("Could not send Message"))
                {
                    resp.setResultCode(NativeConstant.ERROR_CODE_NETWORK_ERROR);
                } 
                else
                {
                    resp.setResultCode(NativeConstant.NATIVE_EXCEPTION);
                }
            }
        }
	    else if (e instanceof SOAPFaultException) 
		{
			String faultCodeString = ((SOAPFaultException) e).getFault()
					.getFaultCode();
			String faultString = ((SOAPFaultException) e).getFault()
					.getFaultString();
			if (faultCodeString.contains(":")) 
			{
				String[] faultCodeStringArr = faultCodeString.split(":");
				if (faultCodeStringArr[1].equals(String
						.valueOf(NativeConstant.SESSION_NOT_CORRECT)))
				{
					resp.setResultCode(NativeConstant.SESSION_NOT_CORRECT);
				} 
				else if (faultCodeStringArr[1].equals(String
						.valueOf(NativeConstant.ACCOUNT_LOCKED)))
				{
					resp.setResultCode(NativeConstant.ACCOUNT_LOCKED);
				}
				else if (faultCodeStringArr[1].equals(String
						.valueOf(NativeConstant.AUTHORIZE_ERRORCODE)))
				{
					resp.setResultCode(NativeConstant.AUTHORIZE_ERRORCODE);
				}
				else if (faultCodeStringArr[1].equals(String
						.valueOf(NativeConstant.SYSTEM_ERRORCODE))) 
				{
					resp.setResultCode(NativeConstant.SYSTEM_ERRORCODE);
				}
				else if (faultCodeString.equals("soap:Client")
						&& faultString.contains("Unmarshalling Error")) 
				{
//					if (faultString.contains("is expected")) 
//					{
//
//						resp.setResultCode(NativeConstant.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
//					} 
//					else
//					{
						resp.setResultCode(NativeConstant.SDK_DATA_INCORRECT_ERRORCODE);
//					}
				} 
				else if (faultString.equals("Connection refused: connect")||faultString.contains("Connection reset")) 
				{
					resp.setResultCode(NativeConstant.ERROR_CODE_NETWORK_ERROR);
				} 
				else if (faultString.contains("handshake_failure") || faultString.contains("trustAnchors parameter must be non-empty")) 
				{
					resp.setResultCode(NativeConstant.SSL_HANDSHAKE_FAILURE);
				} 
				else 
				{
					resp.setResultCode(NativeConstant.NATIVE_EXCEPTION);
				}
			} 
			else
			{
				resp.setResultCode(NativeConstant.NATIVE_EXCEPTION);
			}
		} 
		else 
		{
			if (e.getCause() instanceof SSLHandshakeException) 
			{

				resp.setResultCode(NativeConstant.SSL_HANDSHAKE_FAILURE);
			} 
			else if (e.getCause().getMessage().contains("no protocol")) 
			{
				resp.setResultCode(NativeConstant.NATIVE_URL_FORMAT_ERRORCODE);
			}
			else if (e.getMessage().contains("Could not send Message")) 
			{
				resp.setResultCode(NativeConstant.ERROR_CODE_NETWORK_ERROR);
			}
			else 
			{
				resp.setResultCode(NativeConstant.SYSTEM_ERRORCODE);
			}
		}

	}

	public static Integer processSoapException(Exception e) 
	{
		if (e instanceof SOAPFaultException) 
		{
			String faultCodeString = ((SOAPFaultException) e).getFault()
					.getFaultCode();
			String faultString = ((SOAPFaultException) e).getFault()
					.getFaultString();
			if (faultCodeString.contains(":")) 
			{
				String[] faultCodeStringArr = faultCodeString.split(":");
				if (faultCodeStringArr[1].equals(String
						.valueOf(NativeConstant.SESSION_NOT_CORRECT))) 
				{
					return NativeConstant.SESSION_NOT_CORRECT;
				} 
				else if (faultCodeStringArr[1].equals(String
						.valueOf(NativeConstant.AUTHORIZE_ERRORCODE)))
				{
					return NativeConstant.AUTHORIZE_ERRORCODE;
				} 
				else if (faultCodeStringArr[1].equals(String
						.valueOf(NativeConstant.SYSTEM_ERRORCODE)))
				{
					return NativeConstant.SYSTEM_ERRORCODE;
				} 
				else if (faultCodeString.equals("soap:Client")
						&& faultString.contains("Unmarshalling Error")) 
				{
//					if (faultString.contains("is expected")) 
//					{
//						return NativeConstant.SDK_PARAM_NOT_COMPLETE_ERRORCODE;
//					} 
//					else 
//					{
						return NativeConstant.SDK_DATA_INCORRECT_ERRORCODE;
//					}
				} 
				else if (faultString.equals("Connection refused: connect"))
				{
					return NativeConstant.ERROR_CODE_NETWORK_ERROR;
				} 
				else if (faultString.contains("handshake_failure") || faultString.contains("trustAnchors parameter must be non-empty"))
				{
					return NativeConstant.SSL_HANDSHAKE_FAILURE;
				}
				else {
					return NativeConstant.NATIVE_EXCEPTION;
				}
			} 
			else 
			{
				return NativeConstant.NATIVE_EXCEPTION;
			}
		} 
		else 
		{
			if (e.getCause() instanceof SSLHandshakeException)
			{

				return NativeConstant.SSL_HANDSHAKE_FAILURE;
			} 
			else if ( e.getCause().getMessage().contains("no protocol")) 
			{
				return NativeConstant.NATIVE_URL_FORMAT_ERRORCODE;
			}
			else if (e.getMessage().contains("Could not send Message")) 
			{
				return NativeConstant.ERROR_CODE_NETWORK_ERROR;
			}
			else 
			{
				return NativeConstant.NATIVE_EXCEPTION;
			}
		}
	}
}
